//
//  TokenManager.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/7.
//
import Foundation

enum loginType{
    case apple
    case google
    case email
    case phone
}


class TokenManager {
    static let shared = TokenManager()
    var logintype : loginType = .apple
    private init() {}

    var token: String? {
        // 从 Keychain 或 UserDefaults 获取
        return UserDefaults.standard.string(forKey: "access_token")
    }

    func updateToken(_ token: String?) {
        UserDefaults.standard.set( "Bearer "  + token!, forKey: "access_token")
    }
    
    func updateAppleUserIdentifier(_ userIdentifier : String?){
        UserDefaults.standard.set(userIdentifier, forKey: "appleUserIdentifier")
    }

    func clearToken() {
        UserDefaults.standard.removeObject(forKey: "access_token")
    }
    
    var refreshToken : String? {
        return UserDefaults.standard.string(forKey: "refresh_token")
    }
 
    func updateRefreshToken(_ token: String?){
        guard let   token1 = token else {return}
        UserDefaults.standard.set(token1, forKey: "refresh_token")
    }
    func clearRefreshToken(){
        UserDefaults.standard.removeObject(forKey: "refresh_token")
    }
    
    var appleUserIdentifier : String? {
        return UserDefaults.standard.string(forKey: "appleUserIdentifier")
    }
    func updateAppleToken(_ token: String?){
        UserDefaults.standard.set(token!, forKey: "appleToken")
    }
    var appleToken : String? {
        return UserDefaults.standard.string(forKey: "appleToken")
    }
    
    var appid : String? {
        return UserDefaults.standard.string(forKey: "appid")
    }
    
    func clearAppleToken(){
        UserDefaults.standard.removeObject(forKey: "appleUserIdentifier")
        return UserDefaults.standard.removeObject(forKey: "appleToken")
        
    }
    
    func updateAppid(_ appid : String?){
        UserDefaults.standard.set(appid!, forKey: "appid")
    }
    
    func clearAll(){
        UserDefaults.standard.removeObject(forKey: "appleUserIdentifier")
         UserDefaults.standard.removeObject(forKey: "appleToken")
        UserDefaults.standard.removeObject(forKey: "access_token")
        UserDefaults.standard.removeObject(forKey: "refresh_token")
    }
    
    
    
}
